package ai.leinao.notify.core.configuration;

import ai.leinao.notify.core.NotifyProperties;
import ai.leinao.notify.core.template.TemplateProperties;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 基于配置文件形式的通知属性配置
 * <p>
 * notify:
 *   configs:
 *     email:
 *       - id: alarm
 *         name: 告警
 *         provider: email
 *         notifyType: email
 *         configuration:
 *
 * @author zhong.wang
 * @date 2025/7/8
 */
@Getter
@Setter
@ConfigurationProperties(prefix = "notify")
public class ResourceNotifyProperties {

    /**
     * 通知属性配置
     */
    private Map<String, List<NotifyProperties>> configs = new ConcurrentHashMap<>();

    /**
     * 模板属性配置
     */
    private Map<String, List<TemplateProperties>> templates = new ConcurrentHashMap<>();

    /**
     * 根据通知类型和id获取通知属性
     * @param notifyType
     * @param id
     * @return
     */
    public NotifyProperties getNotifyProperties(String notifyType, String id){
        List<NotifyProperties> notifyProperties = configs.get(notifyType);
        if(CollectionUtils.isEmpty(notifyProperties)){
            return null;
        }
        NotifyProperties result = notifyProperties.stream().filter(notifyProperty -> notifyProperty.getId().equals(id)).findAny().orElse(null);
        return result;
    }

    /**
     * 根据模板类型和id获取模板属性
     * @param notifyType
     * @param id
     * @return
     */
    public TemplateProperties getTemplateProperties(String notifyType, String id){
        List<TemplateProperties> templateProperties = templates.get(notifyType);
        if(CollectionUtils.isEmpty(templateProperties)){
            return null;
        }
        return templateProperties.stream().filter(templateProperty -> templateProperty.getId().equals(id)).findAny().orElse(null);
    }

}
